package moo.stargalaxy_datasyncs.api;

import com.alibaba.fastjson.JSONArray;
import moo.stargalaxy_bukkit.api.GalaxyAPI;
import moo.stargalaxy_bukkit.config.Config;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class SyncAPI {

    public static String sendPost(String url, String secretkey, String data){
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            //实体类
            //构造消息头
            httpPost.setHeader("secretkey", secretkey);
            httpPost.setHeader("Content-Type","application/json; charset=utf-8");
            JSONArray objects = JSONArray.parseArray(data);
            StringEntity postEntity = new StringEntity(objects.toString(), "utf-8");
            // 发送JSON格式的数据请求
            postEntity.setContentType("application/json");
            httpPost.setEntity(postEntity);
            //提交请求
            HttpResponse response = client.execute(httpPost);
            //打印请求返回的code
            System.out.println(response.getStatusLine().getStatusCode());
        } catch (ClientProtocolException e) {
            System.out.println("访问控制台连接异常：无法连接到地址或端口 (Unknown Host)");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("访问控制台连接异常：无法连接到地址或端口 (Connect Error)");
            e.printStackTrace();
        }
        return null;
    }

    public static String sendPost(String url, String param, String secretkey, String data) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url + param);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("secretkey", secretkey);
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            byte[] bytes = data.getBytes();
            conn.setRequestProperty("Content-Length", String.valueOf(bytes.length));
            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
            out.write(data);
            // 发送请求参数
            //out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("访问控制台连接异常：发送POST请求异常 (" + e + ")");
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }
}

